#include "SurfaceExtractProcess.h"
#include "SensorResource.h"
#define DISPATCH_X 64
#define DISPATCH_X_STR "64"
#define GROUP_SIZE 64
#define GROUP_SIZE_STR "64"
#include "QD3D\QD3DTimeQuery.h"

#ifdef QUERY_TIME
//#define SURFACE_EXTRACT_QUERY_TIME
#endif
#ifdef SURFACE_EXTRACT_QUERY_TIME

#define CALL_QUERY_TIME(__str__) timeQuery.addCheckPoint (__str__)

#define GET_DURATION(__name__) \
	timeQuery.getDuration ( \
	std::string (__name__) + "Start", \
	std::string (__name__) + "End")

#define OUTPUT_DURATION(__stream__, __name__) \
	sOut << __name__ << ": " << GET_DURATION (__name__) << "ms\n"

std::ofstream fOut ("..\\Data\\Performance\\test.txt");

#else
#define CALL_QUERY_TIME(__str__)
#endif

#define QUERY_METHOD(__method__, __name__) \
	CALL_QUERY_TIME (std::string (__name__) + "Start"); \
	__method__ (); \
	CALL_QUERY_TIME (std::string (__name__) + "End")

int g_triangleTable[3 * 5 * 256] = {
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 5, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	5, 4, 8, 5, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	1, 4, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 1, 10, 0, 10, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 5, 9, 1, 4, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	5, 1, 10, 5, 10, 8, 5, 8, 9, -1, -1, -1, -1, -1, -1, 
	1, 5, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, 1, 5, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 1, 11, 0, 11, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	1, 4, 8, 1, 8, 9, 1, 9, 11, -1, -1, -1, -1, -1, -1, 
	5, 4, 10, 5, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 5, 11, 0, 11, 10, 0, 10, 8, -1, -1, -1, -1, -1, -1, 
	0, 4, 10, 0, 10, 11, 0, 11, 9, -1, -1, -1, -1, -1, -1, 
	10, 8, 9, 10, 9, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	2, 6, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 6, 0, 6, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 5, 9, 2, 6, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	5, 4, 6, 5, 6, 2, 5, 2, 9, -1, -1, -1, -1, -1, -1, 
	1, 4, 10, 2, 6, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 1, 10, 0, 10, 6, 0, 6, 2, -1, -1, -1, -1, -1, -1, 
	0, 5, 9, 1, 4, 10, 2, 6, 8, -1, -1, -1, -1, -1, -1, 
	5, 1, 10, 5, 10, 6, 5, 6, 2, 5, 2, 9, -1, -1, -1, 
	1, 5, 11, 2, 6, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 6, 0, 6, 2, 1, 5, 11, -1, -1, -1, -1, -1, -1, 
	0, 1, 11, 0, 11, 9, 2, 6, 8, -1, -1, -1, -1, -1, -1, 
	1, 4, 6, 1, 6, 2, 1, 2, 9, 1, 9, 11, -1, -1, -1, 
	5, 4, 10, 5, 10, 11, 2, 6, 8, -1, -1, -1, -1, -1, -1, 
	0, 5, 11, 0, 11, 10, 0, 10, 6, 0, 6, 2, -1, -1, -1, 
	0, 4, 10, 0, 10, 11, 0, 11, 9, 2, 6, 8, -1, -1, -1, 
	2, 6, 10, 2, 10, 11, 2, 11, 9, -1, -1, -1, -1, -1, -1, 
	2, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, 2, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 5, 7, 0, 7, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	5, 4, 8, 5, 8, 2, 5, 2, 7, -1, -1, -1, -1, -1, -1, 
	1, 4, 10, 2, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 1, 10, 0, 10, 8, 2, 7, 9, -1, -1, -1, -1, -1, -1, 
	0, 5, 7, 0, 7, 2, 1, 4, 10, -1, -1, -1, -1, -1, -1, 
	5, 1, 10, 5, 10, 8, 5, 8, 2, 5, 2, 7, -1, -1, -1, 
	1, 5, 11, 2, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, 1, 5, 11, 2, 7, 9, -1, -1, -1, -1, -1, -1, 
	0, 1, 11, 0, 11, 7, 0, 7, 2, -1, -1, -1, -1, -1, -1, 
	1, 4, 8, 1, 8, 2, 1, 2, 7, 1, 7, 11, -1, -1, -1, 
	5, 4, 10, 5, 10, 11, 2, 7, 9, -1, -1, -1, -1, -1, -1, 
	0, 5, 11, 0, 11, 10, 0, 10, 8, 2, 7, 9, -1, -1, -1, 
	0, 4, 10, 0, 10, 11, 0, 11, 7, 0, 7, 2, -1, -1, -1, 
	2, 7, 11, 2, 11, 10, 2, 10, 8, -1, -1, -1, -1, -1, -1, 
	7, 6, 8, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 6, 0, 6, 7, 0, 7, 9, -1, -1, -1, -1, -1, -1, 
	0, 5, 7, 0, 7, 6, 0, 6, 8, -1, -1, -1, -1, -1, -1, 
	5, 4, 6, 5, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	1, 4, 10, 7, 6, 8, 7, 8, 9, -1, -1, -1, -1, -1, -1, 
	0, 1, 10, 0, 10, 6, 0, 6, 7, 0, 7, 9, -1, -1, -1, 
	0, 5, 7, 0, 7, 6, 0, 6, 8, 1, 4, 10, -1, -1, -1, 
	5, 1, 10, 5, 10, 6, 5, 6, 7, -1, -1, -1, -1, -1, -1, 
	1, 5, 11, 7, 6, 8, 7, 8, 9, -1, -1, -1, -1, -1, -1, 
	0, 4, 6, 0, 6, 7, 0, 7, 9, 1, 5, 11, -1, -1, -1, 
	0, 1, 11, 0, 11, 7, 0, 7, 6, 0, 6, 8, -1, -1, -1, 
	1, 4, 6, 1, 6, 7, 1, 7, 11, -1, -1, -1, -1, -1, -1, 
	5, 4, 10, 5, 10, 11, 7, 6, 8, 7, 8, 9, -1, -1, -1, 
	0, 5, 11, 0, 11, 10, 0, 10, 6, 0, 6, 7, 0, 7, 9, 
	0, 4, 10, 0, 10, 11, 0, 11, 7, 0, 7, 6, 0, 6, 8, 
	7, 6, 10, 7, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	3, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, 3, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 5, 9, 3, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	5, 4, 8, 5, 8, 9, 3, 6, 10, -1, -1, -1, -1, -1, -1, 
	1, 4, 6, 1, 6, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 1, 3, 0, 3, 6, 0, 6, 8, -1, -1, -1, -1, -1, -1, 
	0, 5, 9, 1, 4, 6, 1, 6, 3, -1, -1, -1, -1, -1, -1, 
	5, 1, 3, 5, 3, 6, 5, 6, 8, 5, 8, 9, -1, -1, -1, 
	1, 5, 11, 3, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, 1, 5, 11, 3, 6, 10, -1, -1, -1, -1, -1, -1, 
	0, 1, 11, 0, 11, 9, 3, 6, 10, -1, -1, -1, -1, -1, -1, 
	1, 4, 8, 1, 8, 9, 1, 9, 11, 3, 6, 10, -1, -1, -1, 
	5, 4, 6, 5, 6, 3, 5, 3, 11, -1, -1, -1, -1, -1, -1, 
	0, 5, 11, 0, 11, 3, 0, 3, 6, 0, 6, 8, -1, -1, -1, 
	0, 4, 6, 0, 6, 3, 0, 3, 11, 0, 11, 9, -1, -1, -1, 
	3, 6, 8, 3, 8, 9, 3, 9, 11, -1, -1, -1, -1, -1, -1, 
	2, 3, 10, 2, 10, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 10, 0, 10, 3, 0, 3, 2, -1, -1, -1, -1, -1, -1, 
	0, 5, 9, 2, 3, 10, 2, 10, 8, -1, -1, -1, -1, -1, -1, 
	5, 4, 10, 5, 10, 3, 5, 3, 2, 5, 2, 9, -1, -1, -1, 
	1, 4, 8, 1, 8, 2, 1, 2, 3, -1, -1, -1, -1, -1, -1, 
	0, 1, 3, 0, 3, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 5, 9, 1, 4, 8, 1, 8, 2, 1, 2, 3, -1, -1, -1, 
	5, 1, 3, 5, 3, 2, 5, 2, 9, -1, -1, -1, -1, -1, -1, 
	1, 5, 11, 2, 3, 10, 2, 10, 8, -1, -1, -1, -1, -1, -1, 
	0, 4, 10, 0, 10, 3, 0, 3, 2, 1, 5, 11, -1, -1, -1, 
	0, 1, 11, 0, 11, 9, 2, 3, 10, 2, 10, 8, -1, -1, -1, 
	1, 4, 10, 1, 3, 2, 1, 2, 9, 1, 9, 11, -1, -1, -1, 
	5, 4, 8, 5, 8, 2, 5, 2, 3, 5, 3, 11, -1, -1, -1, 
	0, 5, 11, 0, 11, 3, 0, 3, 2, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, 0, 2, 3, 0, 3, 11, 0, 11, 9, -1, -1, -1, 
	2, 3, 11, 2, 11, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	2, 7, 9, 3, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, 2, 7, 9, 3, 6, 10, -1, -1, -1, -1, -1, -1, 
	0, 5, 7, 0, 7, 2, 3, 6, 10, -1, -1, -1, -1, -1, -1, 
	5, 4, 8, 5, 8, 2, 5, 2, 7, 3, 6, 10, -1, -1, -1, 
	1, 4, 6, 1, 6, 3, 2, 7, 9, -1, -1, -1, -1, -1, -1, 
	0, 1, 3, 0, 3, 6, 0, 6, 8, 2, 7, 9, -1, -1, -1, 
	0, 5, 7, 0, 7, 2, 1, 4, 6, 1, 6, 3, -1, -1, -1, 
	5, 1, 3, 5, 3, 6, 5, 6, 8, 5, 8, 2, 5, 2, 7, 
	1, 5, 11, 2, 7, 9, 3, 6, 10, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, 1, 5, 11, 2, 7, 9, 3, 6, 10, -1, -1, -1, 
	0, 1, 11, 0, 11, 7, 0, 7, 2, 3, 6, 10, -1, -1, -1, 
	1, 4, 8, 1, 8, 2, 1, 2, 7, 1, 7, 11, 3, 6, 10, 
	5, 4, 6, 5, 6, 3, 5, 3, 11, 2, 7, 9, -1, -1, -1, 
	0, 5, 11, 0, 11, 3, 0, 3, 6, 0, 6, 8, 2, 7, 9, 
	0, 4, 6, 0, 6, 3, 0, 3, 11, 0, 11, 7, 0, 7, 2, 
	2, 7, 11, 2, 11, 3, 2, 6, 8, -1, -1, -1, -1, -1, -1, 
	7, 3, 10, 7, 10, 8, 7, 8, 9, -1, -1, -1, -1, -1, -1, 
	0, 4, 10, 0, 10, 3, 0, 3, 7, 0, 7, 9, -1, -1, -1, 
	0, 5, 7, 0, 7, 3, 0, 3, 10, 0, 10, 8, -1, -1, -1, 
	5, 4, 10, 5, 10, 3, 5, 3, 7, -1, -1, -1, -1, -1, -1, 
	1, 4, 8, 1, 8, 9, 1, 9, 7, 1, 7, 3, -1, -1, -1, 
	0, 1, 3, 0, 3, 7, 0, 7, 9, -1, -1, -1, -1, -1, -1, 
	0, 5, 7, 0, 7, 3, 0, 3, 1, 0, 4, 8, -1, -1, -1, 
	5, 1, 3, 5, 3, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	1, 5, 11, 7, 3, 10, 7, 10, 8, 7, 8, 9, -1, -1, -1, 
	0, 4, 10, 0, 10, 3, 0, 3, 7, 0, 7, 9, 1, 5, 11, 
	0, 1, 11, 0, 11, 7, 0, 7, 3, 0, 3, 10, 0, 10, 8, 
	1, 4, 10, 1, 3, 7, 1, 7, 11, -1, -1, -1, -1, -1, -1, 
	5, 4, 8, 5, 8, 9, 5, 7, 3, 5, 3, 11, -1, -1, -1, 
	0, 5, 11, 0, 11, 3, 0, 3, 7, 0, 7, 9, -1, -1, -1, 
	0, 4, 8, 7, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	7, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	3, 7, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, 3, 7, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 5, 9, 3, 7, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	5, 4, 8, 5, 8, 9, 3, 7, 11, -1, -1, -1, -1, -1, -1, 
	1, 4, 10, 3, 7, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 1, 10, 0, 10, 8, 3, 7, 11, -1, -1, -1, -1, -1, -1, 
	0, 5, 9, 1, 4, 10, 3, 7, 11, -1, -1, -1, -1, -1, -1, 
	5, 1, 10, 5, 10, 8, 5, 8, 9, 3, 7, 11, -1, -1, -1, 
	1, 5, 7, 1, 7, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, 1, 5, 7, 1, 7, 3, -1, -1, -1, -1, -1, -1, 
	0, 1, 3, 0, 3, 7, 0, 7, 9, -1, -1, -1, -1, -1, -1, 
	1, 4, 8, 1, 8, 9, 1, 9, 7, 1, 7, 3, -1, -1, -1, 
	5, 4, 10, 5, 10, 3, 5, 3, 7, -1, -1, -1, -1, -1, -1, 
	0, 5, 7, 0, 7, 3, 0, 3, 10, 0, 10, 8, -1, -1, -1, 
	0, 4, 10, 0, 10, 3, 0, 3, 7, 0, 7, 9, -1, -1, -1, 
	3, 7, 9, 3, 9, 8, 3, 8, 10, -1, -1, -1, -1, -1, -1, 
	2, 6, 8, 3, 7, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 6, 0, 6, 2, 3, 7, 11, -1, -1, -1, -1, -1, -1, 
	0, 5, 9, 2, 6, 8, 3, 7, 11, -1, -1, -1, -1, -1, -1, 
	5, 4, 6, 5, 6, 2, 5, 2, 9, 3, 7, 11, -1, -1, -1, 
	1, 4, 10, 2, 6, 8, 3, 7, 11, -1, -1, -1, -1, -1, -1, 
	0, 1, 10, 0, 10, 6, 0, 6, 2, 3, 7, 11, -1, -1, -1, 
	0, 5, 9, 1, 4, 10, 2, 6, 8, 3, 7, 11, -1, -1, -1, 
	5, 1, 10, 5, 10, 6, 5, 6, 2, 5, 2, 9, 3, 7, 11, 
	1, 5, 7, 1, 7, 3, 2, 6, 8, -1, -1, -1, -1, -1, -1, 
	0, 4, 6, 0, 6, 2, 1, 5, 7, 1, 7, 3, -1, -1, -1, 
	0, 1, 3, 0, 3, 7, 0, 7, 9, 2, 6, 8, -1, -1, -1, 
	1, 4, 6, 1, 6, 2, 1, 2, 9, 1, 9, 7, 1, 7, 3, 
	5, 4, 10, 5, 10, 3, 5, 3, 7, 2, 6, 8, -1, -1, -1, 
	0, 5, 7, 0, 7, 3, 0, 3, 10, 0, 10, 6, 0, 6, 2, 
	0, 4, 10, 0, 10, 3, 0, 3, 7, 0, 7, 9, 2, 6, 8, 
	2, 6, 10, 2, 10, 3, 2, 7, 9, -1, -1, -1, -1, -1, -1, 
	2, 3, 11, 2, 11, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, 2, 3, 11, 2, 11, 9, -1, -1, -1, -1, -1, -1, 
	0, 5, 11, 0, 11, 3, 0, 3, 2, -1, -1, -1, -1, -1, -1, 
	5, 4, 8, 5, 8, 2, 5, 2, 3, 5, 3, 11, -1, -1, -1, 
	1, 4, 10, 2, 3, 11, 2, 11, 9, -1, -1, -1, -1, -1, -1, 
	0, 1, 10, 0, 10, 8, 2, 3, 11, 2, 11, 9, -1, -1, -1, 
	0, 5, 11, 0, 11, 3, 0, 3, 2, 1, 4, 10, -1, -1, -1, 
	5, 1, 10, 5, 10, 8, 5, 8, 2, 5, 2, 3, 5, 3, 11, 
	1, 5, 9, 1, 9, 2, 1, 2, 3, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, 1, 5, 9, 1, 9, 2, 1, 2, 3, -1, -1, -1, 
	0, 1, 3, 0, 3, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	1, 4, 8, 1, 8, 2, 1, 2, 3, -1, -1, -1, -1, -1, -1, 
	5, 4, 10, 5, 10, 3, 5, 3, 2, 5, 2, 9, -1, -1, -1, 
	0, 5, 9, 0, 2, 3, 0, 3, 10, 0, 10, 8, -1, -1, -1, 
	0, 4, 10, 0, 10, 3, 0, 3, 2, -1, -1, -1, -1, -1, -1, 
	2, 3, 10, 2, 10, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	3, 6, 8, 3, 8, 9, 3, 9, 11, -1, -1, -1, -1, -1, -1, 
	0, 4, 6, 0, 6, 3, 0, 3, 11, 0, 11, 9, -1, -1, -1, 
	0, 5, 11, 0, 11, 3, 0, 3, 6, 0, 6, 8, -1, -1, -1, 
	5, 4, 6, 5, 6, 3, 5, 3, 11, -1, -1, -1, -1, -1, -1, 
	1, 4, 10, 3, 6, 8, 3, 8, 9, 3, 9, 11, -1, -1, -1, 
	0, 1, 10, 0, 10, 6, 0, 6, 3, 0, 3, 11, 0, 11, 9, 
	0, 5, 11, 0, 11, 3, 0, 3, 6, 0, 6, 8, 1, 4, 10, 
	5, 1, 10, 5, 10, 6, 5, 6, 3, 5, 3, 11, -1, -1, -1, 
	1, 5, 9, 1, 9, 8, 1, 8, 6, 1, 6, 3, -1, -1, -1, 
	0, 4, 6, 0, 6, 3, 0, 3, 1, 0, 5, 9, -1, -1, -1, 
	0, 1, 3, 0, 3, 6, 0, 6, 8, -1, -1, -1, -1, -1, -1, 
	1, 4, 6, 1, 6, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	5, 4, 10, 5, 10, 3, 5, 3, 6, 5, 6, 8, 5, 8, 9, 
	0, 5, 9, 3, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 10, 0, 10, 3, 0, 3, 6, 0, 6, 8, -1, -1, -1, 
	3, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	7, 6, 10, 7, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, 7, 6, 10, 7, 10, 11, -1, -1, -1, -1, -1, -1, 
	0, 5, 9, 7, 6, 10, 7, 10, 11, -1, -1, -1, -1, -1, -1, 
	5, 4, 8, 5, 8, 9, 7, 6, 10, 7, 10, 11, -1, -1, -1, 
	1, 4, 6, 1, 6, 7, 1, 7, 11, -1, -1, -1, -1, -1, -1, 
	0, 1, 11, 0, 11, 7, 0, 7, 6, 0, 6, 8, -1, -1, -1, 
	0, 5, 9, 1, 4, 6, 1, 6, 7, 1, 7, 11, -1, -1, -1, 
	5, 1, 11, 5, 7, 6, 5, 6, 8, 5, 8, 9, -1, -1, -1, 
	1, 5, 7, 1, 7, 6, 1, 6, 10, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, 1, 5, 7, 1, 7, 6, 1, 6, 10, -1, -1, -1, 
	0, 1, 10, 0, 10, 6, 0, 6, 7, 0, 7, 9, -1, -1, -1, 
	1, 4, 8, 1, 8, 9, 1, 9, 7, 1, 7, 6, 1, 6, 10, 
	5, 4, 6, 5, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 5, 7, 0, 7, 6, 0, 6, 8, -1, -1, -1, -1, -1, -1, 
	0, 4, 6, 0, 6, 7, 0, 7, 9, -1, -1, -1, -1, -1, -1, 
	7, 6, 8, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	2, 7, 11, 2, 11, 10, 2, 10, 8, -1, -1, -1, -1, -1, -1, 
	0, 4, 10, 0, 10, 11, 0, 11, 7, 0, 7, 2, -1, -1, -1, 
	0, 5, 9, 2, 7, 11, 2, 11, 10, 2, 10, 8, -1, -1, -1, 
	5, 4, 10, 5, 10, 11, 5, 7, 2, 5, 2, 9, -1, -1, -1, 
	1, 4, 8, 1, 8, 2, 1, 2, 7, 1, 7, 11, -1, -1, -1, 
	0, 1, 11, 0, 11, 7, 0, 7, 2, -1, -1, -1, -1, -1, -1, 
	0, 5, 9, 1, 4, 8, 1, 8, 2, 1, 2, 7, 1, 7, 11, 
	5, 1, 11, 5, 7, 2, 5, 2, 9, -1, -1, -1, -1, -1, -1, 
	1, 5, 7, 1, 7, 2, 1, 2, 8, 1, 8, 10, -1, -1, -1, 
	0, 4, 10, 0, 10, 1, 0, 5, 7, 0, 7, 2, -1, -1, -1, 
	0, 1, 10, 0, 10, 8, 0, 2, 7, 0, 7, 9, -1, -1, -1, 
	1, 4, 10, 2, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	5, 4, 8, 5, 8, 2, 5, 2, 7, -1, -1, -1, -1, -1, -1, 
	0, 5, 7, 0, 7, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, 0, 2, 7, 0, 7, 9, -1, -1, -1, -1, -1, -1, 
	2, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	2, 6, 10, 2, 10, 11, 2, 11, 9, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, 2, 6, 10, 2, 10, 11, 2, 11, 9, -1, -1, -1, 
	0, 5, 11, 0, 11, 10, 0, 10, 6, 0, 6, 2, -1, -1, -1, 
	5, 4, 8, 5, 8, 2, 5, 2, 6, 5, 6, 10, 5, 10, 11, 
	1, 4, 6, 1, 6, 2, 1, 2, 9, 1, 9, 11, -1, -1, -1, 
	0, 1, 11, 0, 11, 9, 0, 2, 6, 0, 6, 8, -1, -1, -1, 
	0, 5, 11, 0, 11, 1, 0, 4, 6, 0, 6, 2, -1, -1, -1, 
	5, 1, 11, 2, 6, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	1, 5, 9, 1, 9, 2, 1, 2, 6, 1, 6, 10, -1, -1, -1, 
	0, 4, 8, 1, 5, 9, 1, 9, 2, 1, 2, 6, 1, 6, 10, 
	0, 1, 10, 0, 10, 6, 0, 6, 2, -1, -1, -1, -1, -1, -1, 
	1, 4, 8, 1, 8, 2, 1, 2, 6, 1, 6, 10, -1, -1, -1, 
	5, 4, 6, 5, 6, 2, 5, 2, 9, -1, -1, -1, -1, -1, -1, 
	0, 5, 9, 0, 2, 6, 0, 6, 8, -1, -1, -1, -1, -1, -1, 
	0, 4, 6, 0, 6, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	2, 6, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	10, 8, 9, 10, 9, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 10, 0, 10, 11, 0, 11, 9, -1, -1, -1, -1, -1, -1, 
	0, 5, 11, 0, 11, 10, 0, 10, 8, -1, -1, -1, -1, -1, -1, 
	5, 4, 10, 5, 10, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	1, 4, 8, 1, 8, 9, 1, 9, 11, -1, -1, -1, -1, -1, -1, 
	0, 1, 11, 0, 11, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 5, 11, 0, 11, 1, 0, 4, 8, -1, -1, -1, -1, -1, -1, 
	5, 1, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	1, 5, 9, 1, 9, 8, 1, 8, 10, -1, -1, -1, -1, -1, -1, 
	0, 4, 10, 0, 10, 1, 0, 5, 9, -1, -1, -1, -1, -1, -1, 
	0, 1, 10, 0, 10, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	1, 4, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	5, 4, 8, 5, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 5, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	0, 4, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};

SurfaceExtractProcess::SurfaceExtractProcess ()
{
	_rp_octree = NULL;
	_rp_modelVerticesPack = NULL;
	_rp_modelTrianglesPack = NULL;

	_p_triangleTablePack = NULL;

	_p_calcNodeVertexValueCS = NULL;
	_p_genModelVerticesCS = NULL;
	_p_genTrianglesCS = NULL;
}


SurfaceExtractProcess::~SurfaceExtractProcess ()
{
}

void SurfaceExtractProcess::initialize (Octree *p_octree)
{
	QD3DUtils d3DUtils;
	UINT elemNum = p_octree->structInfo.var.elemNum;
	_p_faceSlotDxConstBuf = d3DUtils.createConstBuffer (
			(FaceSlotConstVars*)NULL);

	ID3D11Buffer *p_triangleTableDxBuf = d3DUtils.createStructuredBuffer (
			5 * 256, (glm::ivec3*)g_triangleTable,
			D3D11_BIND_SHADER_RESOURCE);
	_p_triangleTablePack = new QD3DResourcePack (p_triangleTableDxBuf,
			QD3DResourcePack::SRV);

	CD3D11_BUFFER_DESC infoBufdesc;
	ZeroMemory (&infoBufdesc, sizeof (CD3D11_BUFFER_DESC));
	infoBufdesc.BindFlags = D3D11_BIND_SHADER_RESOURCE |
			D3D11_BIND_UNORDERED_ACCESS;
	infoBufdesc.ByteWidth = 5 * 4;
	infoBufdesc.MiscFlags = D3D11_RESOURCE_MISC_DRAWINDIRECT_ARGS |
			D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS;
	ID3D11Buffer *p_fakeNodesInfoDxBuf = d3DUtils.createBuffer (
			infoBufdesc, NULL);
	_p_fakeNodesInfoPack = new QD3DResourcePack (p_fakeNodesInfoDxBuf);
	ID3D11Buffer *p_fakeNodesDxBuf = d3DUtils.createStructuredBuffer (
			p_octree->structInfo.var.elemNum / 2, (FakeNode*)NULL,
			D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS);
	_p_fakeNodesPack = new QD3DResourcePack (p_fakeNodesDxBuf);
	ID3D11Buffer *p_markDxBuf = d3DUtils.createStructuredBuffer (
			p_octree->structInfo.var.elemNum, (int*)NULL,
			D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS);
	_p_markPack = new QD3DResourcePack (p_markDxBuf);
	ID3D11Buffer *p_ownerNodeIdxDxBuf = d3DUtils.createStructuredBuffer (
			p_octree->structInfo.var.elemNum, (int*)NULL,
			D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS);
	_p_ownerNodeIdxsPack = new QD3DResourcePack (p_ownerNodeIdxDxBuf);
	ID3D11Buffer *p_compactedOwnerNodeIdxDxBuf =
			d3DUtils.createStructuredBuffer (
			p_octree->structInfo.var.elemNum, (int*)NULL,
			D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS);
	_p_compactedOwnerNodeIdxsPack = new QD3DResourcePack (p_compactedOwnerNodeIdxDxBuf);
// 	ID3D11Buffer *p_nodeVerticeValuesDxBuf =
// 			d3DUtils.createStructuredBuffer (
// 			elemNum * 8 * 8, (Octree::NodeVertexValue*)NULL,
// 			D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS,
// 			"NodeVertexValues");
// 	_p_tempNodeVertexValuesPack = new QD3DResourcePack (p_nodeVerticeValuesDxBuf);

	QD3DShaderDesc shaderDesc;
	shaderDesc.setDefine ("TG_NUM_X", DISPATCH_X_STR);
	shaderDesc.setDefine ("VERTEX_STRUCT", VERTEX_HLSL_STRUCT);
	shaderDesc.setDefine ("NODE_STRUCT", Octree::Node::s_HLSL_STRUCT_STR);
	shaderDesc.setDefine ("NODE_VERTEX_STRUCT", Octree::NodeVertex::s_HLSL_STRUCT_STR);
	shaderDesc.setDefine ("NODE_EDGE_STRUCT", Octree::NodeEdge::s_HLSL_STRUCT_STR);
	shaderDesc.setDefine ("NODE_VERTEX_VALUE_STRUCT", Octree::NodeVertexValue::s_HLSL_STRUCT_STR);
	shaderDesc.setDefine ("FAKE_NODE_STRUCT", FakeNode::s_HLSL_STRUCT_STR);
	shaderDesc.setDefine ("GROUP_SIZE", GROUP_SIZE_STR);
	
	_p_calcNodeVertexValueCS =
			d3DUtils.createShaderFromFile< ID3D11ComputeShader > (
			QD3DUtils::COMPUTE_SHADER, "cs_5_0",
			SHADER_FILE ("SurfaceExtract\\CalcNodeVertexValueCS.hlsl"),
			"csMain", shaderDesc);
	_p_genModelVerticesCS =
			d3DUtils.createShaderFromFile< ID3D11ComputeShader > (
			QD3DUtils::COMPUTE_SHADER, "cs_5_0",
			SHADER_FILE ("SurfaceExtract\\GenModelVerticesCS.hlsl"),
			"csMain", shaderDesc);
	_p_genTrianglesCS =
			d3DUtils.createShaderFromFile< ID3D11ComputeShader > (
			QD3DUtils::COMPUTE_SHADER, "cs_5_0",
			SHADER_FILE ("SurfaceExtract\\GenTrianglesCS.hlsl"),
			"csMain", shaderDesc);
	_p_markFakeNodesCS =
			d3DUtils.createShaderFromFile< ID3D11ComputeShader > (
			QD3DUtils::COMPUTE_SHADER, "cs_5_0",
			SHADER_FILE ("SurfaceExtract\\MarkFakeNodesCS.hlsl"),
			"csMain", shaderDesc);
	_p_genFakeNodesCS =
			d3DUtils.createShaderFromFile< ID3D11ComputeShader > (
			QD3DUtils::COMPUTE_SHADER, "cs_5_0",
			SHADER_FILE ("SurfaceExtract\\GenFakeNodesCS.hlsl"),
			"csMain", shaderDesc);
	_p_setFakeNodeVerticesAndEdgesCS =
			d3DUtils.createShaderFromFile< ID3D11ComputeShader > (
			QD3DUtils::COMPUTE_SHADER, "cs_5_0",
			SHADER_FILE ("SurfaceExtract\\SetFakeNodeVerticesAndEdgesCS.hlsl"),
			"csMain", shaderDesc);
	_p_genTrianglesInFakeNodesCS =
			d3DUtils.createShaderFromFile< ID3D11ComputeShader > (
			QD3DUtils::COMPUTE_SHADER, "cs_5_0",
			SHADER_FILE ("SurfaceExtract\\GenTrianglesInFakeNodesCS.hlsl"),
			"csMain", shaderDesc);
	std::ostringstream sOut;
	sOut << elemNum;
	shaderDesc.setDefine ("ELEM_NUM", sOut.str ());
	_p_updateInfoInInitialCS =
			d3DUtils.createShaderFromFile< ID3D11ComputeShader > (
			QD3DUtils::COMPUTE_SHADER, "cs_5_0",
			SHADER_FILE ("SurfaceExtract\\UpdateInfoCS.hlsl"),
			"inInitial", shaderDesc);
	_p_updateInfoInGenFakeNodesCS =
			d3DUtils.createShaderFromFile< ID3D11ComputeShader > (
			QD3DUtils::COMPUTE_SHADER, "cs_5_0",
			SHADER_FILE ("SurfaceExtract\\UpdateInfoCS.hlsl"),
			"inGenFakeNodes", shaderDesc);
	_p_updateInfoInGenTrianglesInFakeNodesCS =
			d3DUtils.createShaderFromFile< ID3D11ComputeShader > (
			QD3DUtils::COMPUTE_SHADER, "cs_5_0",
			SHADER_FILE ("SurfaceExtract\\UpdateInfoCS.hlsl"),
			"inGenTrianglesInFakeNodes", shaderDesc);
	_p_compactPrimitive = new CompactPrimitive (elemNum,
			_p_ownerNodeIdxsPack, _p_markPack,
			_p_compactedOwnerNodeIdxsPack, "int");

}

void SurfaceExtractProcess::process (Octree *p_octree,
		QD3DResourcePack *p_modelVerticesPack,
		QD3DResourcePack *p_modelTrianglesPack)
{
	_rp_octree = p_octree;
	_rp_modelVerticesPack = p_modelVerticesPack;
	_rp_modelTrianglesPack = p_modelTrianglesPack;
#ifdef SURFACE_EXTRACT_QUERY_TIME
	QD3DTimeQuery timeQuery;
	timeQuery.begin ();
	timeQuery.addCheckPoint ("Start");
#endif // SURFACE_EXTRACT_QUERY_TIME
	QUERY_METHOD (_calcNodeVertexValue, "CalcValue");
	QUERY_METHOD (_genModelVertices, "GenVertices");
	QUERY_METHOD (_genTriangles, "GenTriangles");
	QUERY_METHOD (_updateInfoInInitial, "UpdateInfo1");
	QUERY_METHOD (_markAndGenFakeNodes, "GenFakes");
	QUERY_METHOD (_setFakeNodeVerticesAndEdges, "SetFakesVerticesEdges");
	QUERY_METHOD (_updateInfoInGenTrianglesInFakeNodes, "UpdateInfo2");
	QUERY_METHOD (_genTrianglesInFakeNodes, "GenFakeTriangles");
#ifdef SURFACE_EXTRACT_QUERY_TIME
	timeQuery.addCheckPoint ("End");
	timeQuery.end ();
	std::ostringstream sOut;
	sOut << "######################################################" << std::endl;
	sOut << "# All: " << GET_DURATION ("") << "ms\n";
	sOut << "######################################################" << std::endl;
	OUTPUT_DURATION (sOut, "CalcValue");
	OUTPUT_DURATION (sOut, "GenVertices");
	OUTPUT_DURATION (sOut, "GenTriangles");
	OUTPUT_DURATION (sOut, "UpdateInfo1");
	OUTPUT_DURATION (sOut, "GenFakes");
	OUTPUT_DURATION (sOut, "SetFakesVerticesEdges");
	OUTPUT_DURATION (sOut, "UpdateInfo2");
	OUTPUT_DURATION (sOut, "GenFakeTriangles");
	OutputDebugStringA (sOut.str ().c_str ());
#endif // RECONSTRUCT_QUERY_TIME
}

void SurfaceExtractProcess::_calcNodeVertexValue ()
{
	UINT elemNum = _rp_octree->structInfo.var.elemNum;
	QD3DUtils d3DUtils;
	QVector< ID3D11Buffer * > dxConstBufPtrs (1);
	QVector< ID3D11ShaderResourceView * > dxSRVPtrs (3);
	QVector< ID3D11UnorderedAccessView * > dxUAVPtrs (1);

	dxConstBufPtrs[0] = _rp_octree->p_octreeInfoDxConstBuf;
	d3DUtils.setConstBufs (QD3DUtils::COMPUTE_SHADER, 0, dxConstBufPtrs);
	dxSRVPtrs[0] = _rp_octree->p_nodesPack->getSRV ();
	dxSRVPtrs[1] = _rp_octree->p_verticesPack->getSRV ();
	dxSRVPtrs[2] = _rp_octree->p_nodeVerticesPack->getSRV ();
	d3DUtils.setSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs);
	dxUAVPtrs[0] = _rp_octree->p_nodeVerticeValuesPack->getUAV ();
	d3DUtils.setUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs);

	d3DUtils.setComputeShader (_p_calcNodeVertexValueCS);
	d3DUtils.dispatch (elemNum / GROUP_SIZE, 1, 1);
	//d3DUtils.dispatch (DISPATCH_X, elemNum * 2 / DISPATCH_X, 1);
	d3DUtils.clearConstBufs (QD3DUtils::COMPUTE_SHADER, 0,
			dxConstBufPtrs.size ());
	d3DUtils.clearSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs.size ());
	d3DUtils.clearUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs.size ());
}

void SurfaceExtractProcess::_genModelVertices ()
{
	QD3DUtils d3DUtils;
	UINT elemNum = _rp_octree->structInfo.var.elemNum;

	QVector< ID3D11ShaderResourceView * > dxSRVPtrs (2);
	QVector< ID3D11UnorderedAccessView * > dxUAVPtrs (2);

	dxSRVPtrs[0] = _rp_octree->p_nodeVerticesPack->getSRV ();
	dxSRVPtrs[1] = _rp_octree->p_nodeVerticeValuesPack->getSRV ();
	d3DUtils.setSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs);
	dxUAVPtrs[0] = _rp_modelVerticesPack->getUAV ();
	dxUAVPtrs[1] = _rp_octree->p_nodeEdgesPack->getUAV ();
	d3DUtils.setUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs);
	d3DUtils.setComputeShader (_p_genModelVerticesCS);
	d3DUtils.dispatch (elemNum * 12 / 64, 1, 1);
	d3DUtils.clearSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs.size ());
	d3DUtils.clearUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs.size ());
}

void SurfaceExtractProcess::_genTriangles ()
{
	QD3DUtils d3DUtils;
	UINT elemNum = _rp_octree->structInfo.var.elemNum;

	QVector< ID3D11ShaderResourceView * > dxSRVPtrs (5);
	QVector< ID3D11UnorderedAccessView * > dxUAVPtrs (1);

	dxSRVPtrs[0] = _rp_octree->p_nodesPack->getSRV ();
	dxSRVPtrs[1] = _rp_octree->p_nodeVerticeValuesPack->getSRV ();
	dxSRVPtrs[2] = _p_triangleTablePack->getSRV ();
	dxSRVPtrs[3] = _rp_octree->p_levelPack->getSRV ();
	dxSRVPtrs[4] = _rp_modelVerticesPack->getSRV ();
	d3DUtils.setSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs);
	dxUAVPtrs[0] = _rp_modelTrianglesPack->getUAV ();
	d3DUtils.setUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs);
	d3DUtils.setComputeShader (_p_genTrianglesCS);
	d3DUtils.dispatch (elemNum / 64, 1, 1);
	d3DUtils.clearSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs.size ());
	d3DUtils.clearUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs.size ());
}

void SurfaceExtractProcess::clean ()
{
	SAFE_DELETE (_p_triangleTablePack);
	SAFE_RELEASE (_p_calcNodeVertexValueCS);
	SAFE_RELEASE (_p_genModelVerticesCS);
	SAFE_RELEASE (_p_genTrianglesCS);
	_rp_octree = NULL;
	_rp_modelVerticesPack = NULL;
	_rp_modelTrianglesPack = NULL;
}

void SurfaceExtractProcess::_markAndGenFakeNodes ()
{
	QD3DUtils d3DUtils;
	FaceSlotConstVars constVars;
	UINT elemNum = _rp_octree->structInfo.var.elemNum;
	int seq[] = { 0, 2, 4, 1, 3, 5 };
	for (int i = 0; i < 6; ++i) {
		constVars.var.faceSlot = seq[i];
		d3DUtils.updateSubresource (_p_faceSlotDxConstBuf, 0, &constVars);
		_markFakeNodes ();
		_p_compactPrimitive->run (elemNum);
		_updateInfoInGenFakeNodes ();
		_genFakeNodes ();
	}
}

void SurfaceExtractProcess::_markFakeNodes ()
{
	QD3DUtils d3DUtils;
	UINT elemNum = _rp_octree->structInfo.var.elemNum;

	QVector< ID3D11Buffer* > dxConstPtrs (1);
	QVector< ID3D11ShaderResourceView * > dxSRVPtrs (3);
	QVector< ID3D11UnorderedAccessView * > dxUAVPtrs (2);
	dxConstPtrs[0] = _p_faceSlotDxConstBuf;
	d3DUtils.setConstBufs (QD3DUtils::COMPUTE_SHADER, 0, dxConstPtrs);
	dxSRVPtrs[0] = _rp_octree->p_nodesPack->getSRV ();
	dxSRVPtrs[1] = _rp_octree->p_nodeEdgesPack->getSRV ();
	dxSRVPtrs[2] = _rp_octree->p_levelPack->getSRV ();
	d3DUtils.setSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs);
	dxUAVPtrs[0] = _p_markPack->getUAV ();
	dxUAVPtrs[1] = _p_ownerNodeIdxsPack->getUAV ();
	d3DUtils.setUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs);
	d3DUtils.setComputeShader (_p_markFakeNodesCS);
	d3DUtils.dispatch (elemNum / GROUP_SIZE, 1, 1);
	d3DUtils.clearSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs.size ());
	d3DUtils.clearUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs.size ());
	d3DUtils.clearConstBufs (QD3DUtils::COMPUTE_SHADER, 0, dxConstPtrs.size ());
}

void SurfaceExtractProcess::_genFakeNodes ()
{
	QD3DUtils d3DUtils;

	QVector< ID3D11Buffer* > dxConstPtrs (1);
	QVector< ID3D11ShaderResourceView * > dxSRVPtrs (2);
	QVector< ID3D11UnorderedAccessView * > dxUAVPtrs (2);
	dxConstPtrs[0] = _p_faceSlotDxConstBuf;
	d3DUtils.setConstBufs (QD3DUtils::COMPUTE_SHADER, 0, dxConstPtrs);
	dxSRVPtrs[0] = _p_compactedOwnerNodeIdxsPack->getSRV ();
	dxSRVPtrs[1] = _p_fakeNodesInfoPack->getSRV ();
	d3DUtils.setSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs);
	dxUAVPtrs[0] = _rp_octree->p_nodesPack->getUAV ();
	dxUAVPtrs[1] = _p_fakeNodesPack->getUAV ();
	d3DUtils.setUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs);
	d3DUtils.setComputeShader (_p_genFakeNodesCS);
	d3DUtils.indirectDispatch ((ID3D11Buffer*)_p_fakeNodesInfoPack->getRes (), 0);
	d3DUtils.clearSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs.size ());
	d3DUtils.clearUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs.size ());
	d3DUtils.clearConstBufs (QD3DUtils::COMPUTE_SHADER, 0, dxConstPtrs.size ());
}

void SurfaceExtractProcess::_setFakeNodeVerticesAndEdges ()
{
	QD3DUtils d3DUtils;
	UINT elemNum = _rp_octree->structInfo.var.elemNum;
	FaceSlotConstVars constVars;

	QVector< ID3D11Buffer* > dxConstPtrs (1);
	QVector< ID3D11ShaderResourceView * > dxSRVPtrs (3);
	QVector< ID3D11UnorderedAccessView * > dxUAVPtrs (1);

	dxConstPtrs[0] = _p_faceSlotDxConstBuf;
	d3DUtils.setConstBufs (QD3DUtils::COMPUTE_SHADER, 0, dxConstPtrs);
	dxSRVPtrs[0] = _rp_octree->p_nodesPack->getSRV ();
	dxSRVPtrs[1] = _rp_octree->p_nodeVerticeValuesPack->getSRV ();
	dxSRVPtrs[2] = _rp_octree->p_levelPack->getSRV ();
	d3DUtils.setSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs);
	dxUAVPtrs[0] = _p_fakeNodesPack->getUAV ();
	d3DUtils.setUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs);

	for (constVars.var.faceSlot = 0; constVars.var.faceSlot < 6;
			++constVars.var.faceSlot) {
		d3DUtils.updateSubresource (_p_faceSlotDxConstBuf, 0, &constVars);
		d3DUtils.setComputeShader (_p_setFakeNodeVerticesAndEdgesCS);
		d3DUtils.dispatch (elemNum / GROUP_SIZE, 1, 1);
	}
	d3DUtils.clearConstBufs (QD3DUtils::COMPUTE_SHADER, 0, dxConstPtrs.size ());
	d3DUtils.clearSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs.size ());
	d3DUtils.clearUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs.size ());
}

void SurfaceExtractProcess::_genTrianglesInFakeNodes ()
{
	QD3DUtils d3DUtils;

	QVector< ID3D11ShaderResourceView * > dxSRVPtrs (4);
	QVector< ID3D11UnorderedAccessView * > dxUAVPtrs (1);
	dxSRVPtrs[0] = _p_fakeNodesPack->getSRV ();
	dxSRVPtrs[1] = _p_triangleTablePack->getSRV ();
	dxSRVPtrs[2] = _p_fakeNodesInfoPack->getSRV ();
	dxSRVPtrs[3] = _rp_octree->p_levelPack->getSRV ();
	d3DUtils.setSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs);
	dxUAVPtrs[0] = _rp_modelTrianglesPack->getUAV ();
	d3DUtils.setUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs);
	d3DUtils.setComputeShader (_p_genTrianglesInFakeNodesCS);
	d3DUtils.indirectDispatch ((ID3D11Buffer*)_p_fakeNodesInfoPack->getRes (), 0);
	d3DUtils.clearSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs.size ());
	d3DUtils.clearUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs.size ());
}

void SurfaceExtractProcess::_updateInfoInInitial ()
{
	QD3DUtils d3DUtils;

	QVector< ID3D11ShaderResourceView * > dxSRVPtrs (1);
	QVector< ID3D11UnorderedAccessView * > dxUAVPtrs (1);
	dxSRVPtrs[0] = _p_markPack->getSRV ();
	d3DUtils.setSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs);
	dxUAVPtrs[0] = _p_fakeNodesInfoPack->getUAV ();
	d3DUtils.setUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs);
	d3DUtils.setComputeShader (_p_updateInfoInInitialCS);
	d3DUtils.dispatch (1, 1, 1);
	d3DUtils.clearSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs.size ());
	d3DUtils.clearUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs.size ());
}

void SurfaceExtractProcess::_updateInfoInGenFakeNodes ()
{
	QD3DUtils d3DUtils;

	QVector< ID3D11ShaderResourceView * > dxSRVPtrs (1);
	QVector< ID3D11UnorderedAccessView * > dxUAVPtrs (1);
	dxSRVPtrs[0] = _p_markPack->getSRV ();
	d3DUtils.setSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs);
	dxUAVPtrs[0] = _p_fakeNodesInfoPack->getUAV ();
	d3DUtils.setUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs);
	d3DUtils.setComputeShader (_p_updateInfoInGenFakeNodesCS);
	d3DUtils.dispatch (1, 1, 1);
	d3DUtils.clearSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs.size ());
	d3DUtils.clearUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs.size ());
}

void SurfaceExtractProcess::_updateInfoInGenTrianglesInFakeNodes ()
{
	QD3DUtils d3DUtils;

	QVector< ID3D11ShaderResourceView * > dxSRVPtrs (1);
	QVector< ID3D11UnorderedAccessView * > dxUAVPtrs (1);
	dxSRVPtrs[0] = _p_markPack->getSRV ();
	d3DUtils.setSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs);
	dxUAVPtrs[0] = _p_fakeNodesInfoPack->getUAV ();
	d3DUtils.setUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs);
	d3DUtils.setComputeShader (_p_updateInfoInGenTrianglesInFakeNodesCS);
	d3DUtils.dispatch (1, 1, 1);
	d3DUtils.clearSRVs (QD3DUtils::COMPUTE_SHADER, 0, dxSRVPtrs.size ());
	d3DUtils.clearUAVs (QD3DUtils::COMPUTE_SHADER, 0, dxUAVPtrs.size ());
}

const char * FakeNode::s_HLSL_STRUCT_STR = "struct FakeNode"
		"{"
		"	int vertexTypes[8];"
		"	int edgeIdxs[12];"
		"};";
